<template>
  <div class="login">
    <div class="login-box">
      <div class="title">登录</div>
      <div class="form">
        <Input
          v-model="formData.telphone"
          @on-enter="loginClick"
          prefix="ios-phone-portrait"
          placeholder="请输入手机号"
          size="large"
          class="input-item" />
        <Input
          v-model="formData.userName"
          @on-enter="loginClick"
          prefix="ios-person"
          placeholder="请输入密码"
          size="large"
          class="input-item"
          type="password" />
        <Button
          type="primary"
          size="large"
          long
          @click="loginClick">
          登录
        </Button>
      </div>
    </div>
  </div>
</template>

<script>
import MD5 from 'md5'
import { UserCheck } from 'api/config'
import { telphoneStorageKey, userNameStorageKey } from 'common/js/config'
import { setStorage, removeStorage } from 'common/js/utils'

export default {
  created () {
    removeStorage(telphoneStorageKey)
    removeStorage(userNameStorageKey)
  },
  data () {
    return {
      formData: {}
    }
  },
  methods: {
    loginClick () {
      setStorage(telphoneStorageKey, MD5(this.formData.telphone))
      setStorage(userNameStorageKey, MD5(this.formData.userName))

      const bol = UserCheck()

      if (bol) {
        this.$router.push('/home')
      } else {
        this.formData = {}
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f0f2f5;
  background-image: linear-gradient(to bottom right, #2d8cf0, #0081ff);

  .login-box {
    width: 400px;
    padding: 40px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .title {
      margin-bottom: 30px;
      font-size: 24px;
      color: #17233d;
      text-align: center;
      font-weight: 500;
    }

    .form {
      .input-item {
        margin-bottom: 20px;

        .ivu-input-wrapper {
          .ivu-input {
            height: 40px;
            font-size: 14px;

            &:hover, &:focus {
              border-color: #2d8cf0;
            }
          }

          .ivu-input-prefix i {
            font-size: 16px;
            color: #808695;
          }
        }
      }

      .ivu-btn {
        height: 40px;
        font-size: 16px;
        margin-top: 10px;

        &.ivu-btn-primary {
          background: #2d8cf0;
          border-color: #2d8cf0;

          &:hover {
            background: #57a3f3;
            border-color: #57a3f3;
          }
        }
      }
    }
  }
}
</style>
